package com.lib.framework.manager.channel

import android.content.Context
import android.text.TextUtils
import androidx.annotation.NonNull
import com.lib.framework.helper.SharedPreferencesHelper
import com.lib.framework.manager.channel.walle.ChannelReader
import java.io.File

/**
 * Copyright (C), 2020-2020, 享物说
 * FileName: ChannelManager
 * Author: keanbin
 * Date: 2020-02-06 09:47
 * Description: 渠道管理器
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class ChannelManager {

    companion object {

        private var mChannel = ""

        /**
         * 返回市场。  如果获取失败返回""
         *
         * @param context Context
         * @return channel
         */
        fun getChannel(context: Context): String {
            return getChannel(context, "chongwa")
        }

        /**
         * 返回市场。  如果获取失败返回defaultChannel
         *
         * @param context        Context
         * @param defaultChannel 默认渠道
         * @return channel
         */
        fun getChannel(context: Context, defaultChannel: String): String {
            //内存中获取
            if (!TextUtils.isEmpty(mChannel)) {
                return mChannel
            }

            //sp中获取
            mChannel = getChannelBySharedPreferences(context)
            if (!TextUtils.isEmpty(mChannel)) {
                return mChannel
            }

            //从apk中获取
            mChannel = getChannelFromApk(context)
            if (!TextUtils.isEmpty(mChannel)) {
                //保存sp中备用
                saveChannelBySharedPreferences(context, mChannel)
                return mChannel
            }

            //全部获取失败
            return defaultChannel
        }

        /**
         * 从 apk 中获取渠道号
         *
         * @param context
         * @return
         */
        private fun getChannelFromApk(@NonNull context: Context): String {
            val apkPath = getApkPath(context)
            if (apkPath.isNullOrEmpty()) {
                return ""
            }
            val channelInfo = ChannelReader.get(File(apkPath)) ?: return ""
            return channelInfo.channel ?: ""
        }

        /**
         * 获取 apk 的目录
         *
         * @param context
         * @return
         */
        private fun getApkPath(@NonNull context: Context): String? {
            var apkPath: String? = null
            try {
                val applicationInfo = context.applicationInfo ?: return null
                apkPath = applicationInfo.sourceDir
            } catch (e: Throwable) {
            }

            return apkPath
        }

        /**
         * 本地保存channel
         *
         * @param context
         * @param channel
         */
        private fun saveChannelBySharedPreferences(context: Context, channel: String) {
            SharedPreferencesHelper(context, "sp_channel").putString("sp_key_channel", channel)
        }

        /**
         * 从sp中获取channel
         *
         * @param context
         */
        private fun getChannelBySharedPreferences(context: Context): String {
            return SharedPreferencesHelper(context, "sp_channel").getString("sp_key_channel", "") ?: ""
        }
    }
}